/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.gui.blocks.parametrized;

import bobbietalk.Utilities;
import bobbietalk.xsd.Palette;
import java.util.List;

/**
 *
 * @author roberto
 */
public abstract class TypeSpec {

    private String name;

    protected TypeSpec(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static TypeSpec buildFromXsdAttribute(Palette.BlockClass.Attributes.Attribute a) {

        TypeSpec res = null;

        String type = a.getType();

        if (type.equals("int")) {

            res = new IntTypeSpec();
            
            if (a.getRange() != null) {
                if (a.getRange().getMin() != null) {
                    
                    ((IntTypeSpec)res).setMinValue(a.getRange().getMin().intValue());
                    
                }
                
                if (a.getRange().getMax() != null) {
                    
                    ((IntTypeSpec)res).setMaxValue(a.getRange().getMax().intValue());
                    
                }
            }

        } else if (type.equals("string")) {

            res = new StringTypeSpec();

        } else if (type.equals("enum")) {

            EnumTypeSpec eres = new EnumTypeSpec();
            res = eres;

            for (Palette.BlockClass.Attributes.Attribute.Values.Value v : a.getValues().getValue()) {

                eres.add(v.getName(), enumForCurrentLanguage(v.getLabel()));

            }


        }

        return res;
    }

    public abstract String getStringForValue(Object value);
    
    protected static String enumForCurrentLanguage(List<Palette.BlockClass.Attributes.Attribute.Values.Value.Label> labels) {

        final String country = Utilities.getCountryCode();

        for (Palette.BlockClass.Attributes.Attribute.Values.Value.Label l : labels) {


            if (l.getLang().equals(country)) {

                return l.getValue();

            }

        }

        throw new IllegalArgumentException("no label for current local:" + country);

    }
}
